// VideoEditorCtrl.cpp : Implementation of the CVideoEditorCtrl ActiveX Control class.

#include "stdafx.h"
#include "VideoEditor.h"
#include "VideoEditorCtrl.h"
#include "VideoEditorPropPage.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#endif 




IMPLEMENT_DYNCREATE(CVideoEditorCtrl, COleControl)



// Message map

BEGIN_MESSAGE_MAP(CVideoEditorCtrl, COleControl)
	ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
END_MESSAGE_MAP()



// Dispatch map

BEGIN_DISPATCH_MAP(CVideoEditorCtrl, COleControl)
	DISP_FUNCTION_ID(CVideoEditorCtrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION_ID(CVideoEditorCtrl, "Open", dispidOpen, Open, VT_I4, VTS_BSTR VTS_I4 VTS_I4)
	DISP_FUNCTION_ID(CVideoEditorCtrl, "Play", dispidPlay, Play, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION_ID(CVideoEditorCtrl, "Close", dispidClose, Close, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION_ID(CVideoEditorCtrl, "SetCurrentPosition", dispidSetCurrentPosition, SetCurrentPosition, VT_EMPTY, VTS_I4)
	DISP_FUNCTION_ID(CVideoEditorCtrl, "Pause", dispidPause, Pause, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION_ID(CVideoEditorCtrl, "Stop", dispidStop, Stop, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION_ID(CVideoEditorCtrl, "SnapshotBitmap", dispidSnapshotBitmap, SnapshotBitmap, VT_I4, VTS_BSTR)
	DISP_FUNCTION_ID(CVideoEditorCtrl, "GetCurrentPlayTime", dispidGetCurrentPlayTime, GetCurrentPlayTime, VT_R8, VTS_NONE)
END_DISPATCH_MAP()



// Event map

BEGIN_EVENT_MAP(CVideoEditorCtrl, COleControl)
	EVENT_CUSTOM_ID("PlayComplete", eventidPlayComplete, PlayComplete, VTS_NONE)
	EVENT_CUSTOM_ID("PlayAbort", eventidPlayAbort, PlayAbort, VTS_NONE)
END_EVENT_MAP()



// Property pages

// TODO: Add more property pages as needed.  Remember to increase the count!
BEGIN_PROPPAGEIDS(CVideoEditorCtrl, 1)
	PROPPAGEID(CVideoEditorPropPage::guid)
END_PROPPAGEIDS(CVideoEditorCtrl)



// Initialize class factory and guid

IMPLEMENT_OLECREATE_EX(CVideoEditorCtrl, "VIDEOEDITOR.VideoEditorCtrl.1",
	0xc61681a9, 0xb34a, 0x43f6, 0xa1, 0xe9, 0x21, 0x4f, 0x28, 0x91, 0xaf, 0x21)



// Type library ID and version

IMPLEMENT_OLETYPELIB(CVideoEditorCtrl, _tlid, _wVerMajor, _wVerMinor)



// Interface IDs

const IID BASED_CODE IID_DVideoEditor =
		{ 0x8098692D, 0xD012, 0x48C9, { 0x80, 0x60, 0xBF, 0xEC, 0x54, 0x64, 0xCE, 0xEA } };
const IID BASED_CODE IID_DVideoEditorEvents =
		{ 0x95EA8280, 0xE252, 0x448B, { 0x93, 0xE2, 0xF3, 0x24, 0x32, 0x5F, 0x3E, 0x36 } };



// Control type information

static const DWORD BASED_CODE _dwVideoEditorOleMisc =
	OLEMISC_ACTIVATEWHENVISIBLE |
	OLEMISC_SETCLIENTSITEFIRST |
	OLEMISC_INSIDEOUT |
	OLEMISC_CANTLINKINSIDE |
	OLEMISC_RECOMPOSEONRESIZE;

IMPLEMENT_OLECTLTYPE(CVideoEditorCtrl, IDS_VIDEOEDITOR, _dwVideoEditorOleMisc)



// CVideoEditorCtrl::CVideoEditorCtrlFactory::UpdateRegistry -
// Adds or removes system registry entries for CVideoEditorCtrl

BOOL CVideoEditorCtrl::CVideoEditorCtrlFactory::UpdateRegistry(BOOL bRegister)
{
	// TODO: Verify that your control follows apartment-model threading rules.
	// Refer to MFC TechNote 64 for more information.
	// If your control does not conform to the apartment-model rules, then
	// you must modify the code below, changing the 6th parameter from
	// afxRegApartmentThreading to 0.

	if (bRegister)
		return AfxOleRegisterControlClass(
			AfxGetInstanceHandle(),
			m_clsid,
			m_lpszProgID,
			IDS_VIDEOEDITOR,
			IDB_VIDEOEDITOR,
			afxRegApartmentThreading,
			_dwVideoEditorOleMisc,
			_tlid,
			_wVerMajor,
			_wVerMinor);
	else
		return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
}


// CVideoEditorCtrl::CVideoEditorCtrl - Constructor

void RecvPlayerMessage(void* pVoid, MSG_TYPE msg_type)
{
	CVideoEditorCtrl* pThis = (CVideoEditorCtrl*)pVoid;
	if(pThis == NULL)
		return;

	pThis->OnRecvPlayerMessage(msg_type);
}

CVideoEditorCtrl::CVideoEditorCtrl()
:m_player(this, RecvPlayerMessage)
{
	InitializeIIDs(&IID_DVideoEditor, &IID_DVideoEditorEvents);
	// TODO: Initialize your control's instance data here.
}



// CVideoEditorCtrl::~CVideoEditorCtrl - Destructor

CVideoEditorCtrl::~CVideoEditorCtrl()
{
	// TODO: Cleanup your control's instance data here.
}


void CVideoEditorCtrl::OnRecvPlayerMessage(MSG_TYPE msg_type)
{
	switch (msg_type)
	{
	case COMPLETE:
		PlayComplete();
		break;
	case USERABORT:
	case ERRORABORT:
		PlayAbort();
		break;
	default:
		break;
	}
}


// CVideoEditorCtrl::OnDraw - Drawing function

void CVideoEditorCtrl::OnDraw(
			CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{
	if (!pdc)
		return;

	// TODO: Replace the following code with your own drawing code.
	pdc->FillRect(rcBounds, CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH)));
	pdc->Ellipse(rcBounds);
}



// CVideoEditorCtrl::DoPropExchange - Persistence support

void CVideoEditorCtrl::DoPropExchange(CPropExchange* pPX)
{
	ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
	COleControl::DoPropExchange(pPX);

	// TODO: Call PX_ functions for each persistent custom property.
}



// CVideoEditorCtrl::OnResetState - Reset control to default state

void CVideoEditorCtrl::OnResetState()
{
	COleControl::OnResetState();  // Resets defaults found in DoPropExchange

	// TODO: Reset any other control state here.
}



// CVideoEditorCtrl::AboutBox - Display an "About" box to the user

void CVideoEditorCtrl::AboutBox()
{
	CDialog dlgAbout(IDD_ABOUTBOX_VIDEOEDITOR);
	dlgAbout.DoModal();
}

// CVideoEditorCtrl message handlers

LONG CVideoEditorCtrl::Open(LPCTSTR fileName, LONG hWndDisplay, LONG hWndNotify)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	return (LONG)m_player.Open(fileName, (HWND)hWndDisplay);
}

void CVideoEditorCtrl::Play(void)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	m_player.Play();
}

void CVideoEditorCtrl::Close(void)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	m_player.Close();
}

void CVideoEditorCtrl::SetCurrentPosition(LONG inPosition)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	m_player.SetCurrentPosition(inPosition);
}

void CVideoEditorCtrl::Pause(void)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	m_player.Pause();
}

void CVideoEditorCtrl::Stop(void)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	m_player.Stop();
}

LONG CVideoEditorCtrl::SnapshotBitmap(LPCTSTR outFile)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	return (LONG)m_player.SnapshotBitmap(outFile);
}

DOUBLE CVideoEditorCtrl::GetCurrentPlayTime(void)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	return m_player.GetCurrentPlayTime();
}
